package com.znf4.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.znf4.config.MqConfig;
import com.znf4.entity.PollingParam;
import com.znf4.id.IdGenerator;
import com.znf4.notify.entity.RpNotifyRecord;
import com.znf4.notify.entity.RpNotifyRecordLog;
import com.znf4.notify.enums.NotifyStatusEnum;
import com.znf4.notify.enums.NotifyTypeEnum;
import com.znf4.notify.service.RpNotifyService;
import com.znf4.page.PageBean;
import com.znf4.page.PageParam;
import com.znf4.trade.enums.TradeStatusEnum;

/**
 * <b>功能说明:
 * TODO... 无注释
 */
@Service("pollingPersist")
public class PollingPersist {

    private static final Logger LOG = LoggerFactory.getLogger(PollingPersist.class);

    @Autowired
    private RpNotifyService rpNotifyService;

    @Autowired
    private PollingParam pollingParam;

    @Autowired
    private PollingQueue pollingQueue;

    @Autowired
    private JmsTemplate jmsTemplate;
    
    @Autowired
    private IdGenerator idGenerator;
    



    /**
     * 从数据库中取一次数据用来当系统启动时初始化
     */
    public void initNotifyDataFromDB() {
        LOG.info("===>init get notify data from database");

        int pageNum = 1; // 当前页
        int numPerPage = 10; // 每页记录数
        PageParam pageParam = new PageParam(pageNum, numPerPage);

        List<RpNotifyRecord> recordList = new ArrayList<RpNotifyRecord>(); // 每次拿到的结果集

        // 组装查询条件，通知状态不成功，并且还没有超过最大通知次数的
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("statusNotSuccess", "statusNotSuccess"); // 不是成功状态
        paramMap.put("statusNotFailed", "statusNotFailed"); // 不是失败状态
        paramMap.put("maxNotifyTimes", pollingParam.getMaxNotifyTimes());

        List<String> notifyTypeList = new ArrayList<String>();
        notifyTypeList.add(NotifyTypeEnum.PROXY_PAY_RESULT_SEARCH.name());//代付结果查询
        notifyTypeList.add(NotifyTypeEnum.QUICK_PAY_RESULT_SEARCH.name());//快捷支付查询
        notifyTypeList.add(NotifyTypeEnum.DEDUCT_PAY_RESULT_SEARCH.name());//代扣支付查询
        paramMap.put("notifyTypeList", notifyTypeList);//通知类型为代付结果查询
//        paramMap.put("notifyType", NotifyTypeEnum.PROXY_PAY_RESULT_SEARCH.name());//通知类型为代付结果查询

        // >>>>>>>>>> 查第一页开始 >>>>>>>>>>
        LOG.info("==>pageNum:" + pageNum + ", numPerPage:" + numPerPage);
        PageBean<RpNotifyRecord> pageBean = null ; // rpNotifyService.queryNotifyRecordListPage(pageParam, paramMap);
        recordList = pageBean.getRecordList();
        if (recordList == null || recordList.isEmpty()) {
            LOG.info("==>recordList is empty");
            return;
        }
        LOG.info("==>now page size:" + recordList.size());

        int totalPage = pageBean.getTotalPage(); //总页数
        int totalCount = pageBean.getTotalCount(); // 总记录数
        LOG.info("===>totalPage:" + totalPage);
        LOG.info("===>totalCount:" + totalCount);

        for (RpNotifyRecord notifyRecord : recordList) {
            pollingQueue.addToNotifyTaskDelayQueue(notifyRecord);
        }

        // 如果有第2页或以上页
        for (pageNum = 2; pageNum <= totalPage; pageNum++) {
            LOG.info("==>pageNum:" + pageNum + ", numPerPage:" + numPerPage);
//            pageBean = rpNotifyService.queryNotifyRecordListPage(new PageParam(pageNum, numPerPage), paramMap);
            recordList = pageBean.getRecordList();
            if (recordList == null || recordList.isEmpty()) {
                LOG.info("==>recordList is empty");
                return;
            }
            LOG.info("==>now page size:" + recordList.size());

            for (RpNotifyRecord notifyRecord : recordList) {
                pollingQueue.addToNotifyTaskDelayQueue(notifyRecord);
            }
        }
    }

}
